import { createStore, Plugin, PluginOnDocumentLoad } from "../../core";

import AttachmentListWithStore from "./AttachmentListWithStore";
import StoreProps from "./StoreProps";
import { ReactElement, useMemo } from "react";

interface AttachmentPlugin extends Plugin {
  Attachments: () => ReactElement;
  onDocumentLoad: (props: PluginOnDocumentLoad) => void;
}

const attachmentPlugin = (): AttachmentPlugin => {
  const store = useMemo(() => createStore<StoreProps>({}), []);

  const AttachmentsDecorator = () => <AttachmentListWithStore store={store} />;

  return {
    onDocumentLoad: (props: PluginOnDocumentLoad) => {
      store.update("doc", props.doc);
    },
    Attachments: AttachmentsDecorator,
  };
};

export default attachmentPlugin;
